package com.piece.blog.service;

import com.piece.blog.model.TrainingQuestionTags;
import com.piece.blog.repository.TrainingQuestionTagsRepository;
import com.piece.core.framework.support.bootstrap.TreeData;
import com.piece.core.framework.support.convert.Convert;
import com.piece.core.framework.util.string.StringUtil;
import com.piece.core.jpa.repository.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class TrainingQuestionTagsService extends BaseService<TrainingQuestionTags, String> {

    @Autowired
    private TrainingQuestionTagsRepository trainingQuestionTagsRepository;

    @PostConstruct
    public void initRepository() {
        setRepository(trainingQuestionTagsRepository);
    }

    @Override
    public TrainingQuestionTags insert(TrainingQuestionTags tags) {
        if (null != tags.getParentId() && 0 != tags.getParentId()) {
            TrainingQuestionTags pTag = trainingQuestionTagsRepository.findById(tags.getParentId());
            if (StringUtil.isEmpty(pTag.getAncestors())) {
                tags.setAncestors(Convert.toStr(tags.getParentId(), "0"));
            } else {
                tags.setAncestors(pTag.getAncestors() + "," + tags.getParentId());
            }
        } else {
            tags.setAncestors(null);
        }

        return trainingQuestionTagsRepository.insert(tags);
    }

    @Override
    public void update(TrainingQuestionTags tags) {
        if (null != tags.getParentId() && 0 != tags.getParentId()) {
            TrainingQuestionTags pTag = trainingQuestionTagsRepository.findById(tags.getParentId());
            if (null != pTag) {
                String ancestors = Convert.toStr(tags.getParentId());
                if (StringUtil.isEmpty(pTag.getAncestors())) {
                    if ("0".equals(ancestors)) {
                        ancestors = null;
                    }
                } else {
                    ancestors = pTag.getAncestors() + "," + pTag.getTagId();
                }
                tags.setAncestors(ancestors);
                updateDictChildren(tags.getTagId(), ancestors);
            }
        }

        trainingQuestionTagsRepository.updateNotNull(tags);
    }

    /**
     * 修改子关系
     *
     * @param tagId  标签ID
     * @param ancestors 祖级列表
     */
    public void updateDictChildren(Long tagId, String ancestors) {
        ancestors = ancestors == null ? "" : (ancestors + ",");
        HashMap<String, Object> condition = new HashMap<>();
        condition.put("parentId", tagId);
        List<TrainingQuestionTags> children = trainingQuestionTagsRepository.findByCondition(condition, null);
        for (TrainingQuestionTags child : children) {
            child.setAncestors(ancestors + tagId);
        }
        if (children.size() > 0) {
            trainingQuestionTagsRepository.batchUpdate(children);
        }
    }

    /**
     * 查询字典树
     */
    public List<TreeData> findTreeData(List<TrainingQuestionTags> dataList) {
        return findTreeData(dataList, false, null);
    }

    /**
     * 对象转字典树
     *
     * @param dataList     字典列表
     * @param isCheck      是否需要选中
     * @param selectedList 已存在字典列表
     * @return
     */
    public List<TreeData> findTreeData(List<TrainingQuestionTags> dataList, boolean isCheck, List<String> selectedList) {
        List<TreeData> trees = new ArrayList<>();
        boolean checked;
        for (TrainingQuestionTags tag : dataList) {
            TreeData treeData = TreeData.builder()
                    .id(String.valueOf(tag.getTagId()))
                    .pId(String.valueOf(tag.getParentId()))
                    .title(tag.getTagName())
                    .name(tag.getTagName())
                    .build();

            if (isCheck) {
                checked = selectedList.contains(tag.getTagId().toString());
                treeData.setChecked(checked);
            } else {
                treeData.setChecked(false);
            }
            trees.add(treeData);
        }
        return trees;
    }

}
